package com.zeng.controller;

import com.zeng.service.GoodsService;
import com.zeng.entity.Goods;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Goods)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-17 19:17:03
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Goods selectOne(Integer id) {
        return this.goodsService.queryById(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Goods> selectAll() {
        return this.goodsService.selectAll();
    }

    /**
     * 获取首页在售商品
     *
     * @return
     */
    @GetMapping("selectShowIndex")
    public List<Goods> selectShowIndex() {
        Goods goods = new Goods();
        goods.setDeleteflag(0);
        goods.setIsreview(1);
        return this.goodsService.queryAll(goods);
    }

    /**
     * 查询结果页面
     *
     * @return
     */
    @PostMapping("selectResult")
    public Result selectResult(@RequestBody HashMap<String, String> map) {
        System.out.println("--------");
        System.out.println("kind=" + map.get("kind"));
        System.out.println("searchkey=" + map.get("searchkey"));
        System.out.println("--------");
        if (map.get("searchkey") != null) {
            if (map.get("searchkey").equals("") == false) {
                System.out.println("关键字查找");
                return Result.success("查询关键词成功", this.goodsService.UserselectAllByKey(map.get("searchkey")));
            }
        }
        if (map.get("kind") != null) {
            if (map.get("kind").equals("") == false) {
                System.out.println("种类查找");
                Goods goods = new Goods();
                goods.setIsreview(1);
                goods.setKid(Integer.valueOf(map.get("kind")));
                System.out.println(this.goodsService.queryAll(goods));
                return Result.success("查询类型成功", this.goodsService.queryAll(goods));
            }
        }
        Goods goods = new Goods();
        goods.setIsreview(1);
        goods.setDeleteflag(0);
        return Result.success("查询所有", this.goodsService.queryAll(goods));
    }

    /**
     * 获取商家自营商品
     *
     * @return
     */
    @GetMapping("selectShowZiYing")
    public List<Goods> selectShowZiYing() {
        Goods goods = new Goods();
        goods.setDeleteflag(0);
        goods.setIsreview(1);
        goods.setUid(1001);
        return this.goodsService.queryAll(goods);
    }

    @GetMapping("selectByUid")
    public List<Goods> selectByUid(Integer id) {
        Goods goods = new Goods();
        goods.setDeleteflag(0);
        goods.setIsreview(1);
        goods.setUid(id);
        return this.goodsService.queryAll(goods);
    }


    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.goodsService.queryCount();
        List<Goods> list = this.goodsService.queryAllByLimit(offset, limit);
        ResultPage resultPage = new ResultPage();
        resultPage.setData(list);
        resultPage.setPageTotal(pageTotal);
        return resultPage;
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        ResultPage resultPage = new ResultPage();
//        if (map.get("key").equals("")) {
//            int pageTotal = this.goodsService.queryCount();
//            List<Goods> list = this.goodsService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
//            resultPage.setData(list);
//            resultPage.setPageTotal(pageTotal);
//        } else {
        int pageTotal = this.goodsService.queryCountSelect(map.get("key"));
        List<Goods> list = this.goodsService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
        resultPage.setData(list);
        resultPage.setPageTotal(pageTotal);
//        }
        return resultPage;
    }

    @GetMapping("deleteById")
    public Result deleteById(Integer id) {
        this.goodsService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("deleteByFlag")
    public String deleteByFlag(Integer id) {
        Goods goods = goodsService.queryById(id);
        goods.setDeleteflag(1);
        goodsService.update(goods);
        return null;
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Goods goods = this.goodsService.queryById(Integer.valueOf(map.get("goodsid")));
        goods.setKid(Integer.valueOf(map.get("kid")));
        goods.setIsreview(0);
        goods.setDeleteflag(0);
        goods.setPrice(Double.valueOf(map.get("price")));
        goods.setGoodsname(map.get("goodsname"));
        goods.setSpec(map.get("spec"));
        goods.setIconurl(map.get("iconurl"));
        goods.setDetails(map.get("details"));
        goods.setCreatetime(TimeUtils.NowTimeN());
        goods.setGoodstitle(map.get("goodstitle"));
        goodsService.update(goods);
        return Result.success("修改成功");
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Goods goods = new Goods();
        goods.setKid(Integer.valueOf(map.get("kid")));
        goods.setUid(Integer.valueOf(map.get("uid")));
        goods.setIsreview(0);
        goods.setDeleteflag(0);
        goods.setPrice(Double.valueOf(map.get("price")));
        goods.setGoodsname(map.get("goodsname"));
        goods.setSpec(map.get("spec"));
        goods.setIconurl(map.get("iconurl"));
        goods.setDetails(map.get("details"));
        goods.setCreatetime(TimeUtils.NowTimeN());
        goods.setGoodstitle(map.get("goodstitle"));
        goodsService.insert(goods);
        return Result.success("添加成功");
    }


    /**
     * 下面是用户专用接口
     */
    @GetMapping("getGoodsByUid")
    public List<Goods> getGoodsByUid(Integer id) {
        Goods goods = new Goods();
        goods.setDeleteflag(0);
        goods.setUid(id);
        return this.goodsService.queryAll(goods);
    }

    @RequestMapping("useradd")
    public Result useradd(@RequestBody HashMap<String, String> map
    ) {
        Goods goods = new Goods();
        goods.setKid(Integer.valueOf(map.get("kid")));
        goods.setUid(Integer.valueOf(map.get("uid")));
        goods.setIsreview(0);
        goods.setDeleteflag(0);
        goods.setPrice(Double.valueOf(map.get("price")));
        goods.setGoodsname(map.get("goodsname"));
        goods.setSpec(map.get("spec"));
        goods.setIconurl(map.get("iconurl"));
        goods.setDetails(map.get("details"));
        goods.setCreatetime(TimeUtils.NowTimeN());
        goods.setGoodstitle(map.get("goodstitle"));
        goodsService.insert(goods);
        return Result.success("添加成功");
    }

    @RequestMapping("useredit")
    public Result useredit(@RequestBody HashMap<String, String> map
    ) {
        Goods goods = this.goodsService.queryById(Integer.valueOf(map.get("goodsid")));
        goods.setKid(Integer.valueOf(map.get("kid")));
        goods.setIsreview(0);
        goods.setDeleteflag(0);
        goods.setPrice(Double.valueOf(map.get("price")));
        goods.setGoodsname(map.get("goodsname"));
        goods.setSpec(map.get("spec"));
        goods.setIconurl(map.get("iconurl"));
        goods.setDetails(map.get("details"));
        goods.setCreatetime(TimeUtils.NowTimeN());
        goods.setGoodstitle(map.get("goodstitle"));
        goodsService.update(goods);
        return Result.success("修改成功");
    }

    /**
     * 下面是管理员专用接口
     */

    @GetMapping("PassAudit")
    public Result PassAudit(Integer id) {
        Goods goods = this.goodsService.queryById(id);
        goods.setIsreview(1);
        this.goodsService.update(goods);
        return Result.success("审核通过");
    }

    @GetMapping("ReMoveAudit")
    public Result ReMoveAudit(Integer id) {
        Goods goods = this.goodsService.queryById(id);
        goods.setIsreview(2);
        this.goodsService.update(goods);
        return Result.success("审核驳回");
    }


}